--取得授权验证网址

local httparg               = require("shared.api.httparg")
local response              = require("shared.api.response")
local config                = require("config")
local httpclient            = require "shared.http.client"
local util                  = require "proxy.game.common.util"
local tag                   = httparg.tag()
local assertion             = httparg.assertion

local ngx                   = _G.ngx
local get_response_spec     = util.get_response_spec
local fill_response_header  = util.fill_response_header
local is_match_content_type = util.is_match_content_type
local get_content_decoder   = util.get_content_decoder
local get_method            = util.get_method


local accountGateWayUrl=config.ENV.ACCOUNTGATE_SERVICE

local ARGS = {
  tag.body('json', 'required'),

  redirect_uri  = tag.json.redirect_uri  ('string', 'required'),
  state         = tag.json.state         ('string', 'required'),
}


--根ulr
local ADDRESS = accountGateWayUrl
--后面接的path
local ACTION  = string.format("%s/auth/url",ngx.var.oauth_provider)

  if not ADDRESS or not ACTION then
    ngx.status = ngx.HTTP_NOT_FOUND
    ngx.exit(ngx.OK)
  end

--从rgs取实际的url
do
  local location = ngx.unescape_uri(ADDRESS..ACTION)
  local method   = "PEEK"
  local headers  = ngx.req.get_headers()

  --取消keep alive设置
  headers.Connection="close"


  ngx.req.read_body()
  local data   = ngx.req.get_body_data()

  local query  = ngx.var.args

  do

    headers['X-Forwarded-For'] = ngx.var.proxy_add_x_forwarded_for
  end


  --移除方法设定
  local rmvLowKey="x-http-method-override"
  --移除gzip设定
  local rmvKey2="accept-encoding"
  for k,v in pairs(headers) do
     local low=string.lower(k)
     if low==rmvLowKey  or low==rmvKey2   then
       headers[k]=nil
     end
  end

  -- perform http request
  local resp, err = httpclient.new()
    :uri(location)
    :headers(headers)
    :query(query)
    :body(data)
    :send(method, 2000, nil)

  if err or (not resp) then
    ngx.status = 500
    ngx.say(err)
    ngx.exit(ngx.OK)
  end

  -- response
  ngx.header['Server']       = ngx.var.SERVICE_NAME
  ngx.header['Content-Type'] = resp.headers['Content-Type']

  local rmvLowKey="content-length"

  for k,v in pairs(resp.headers) do
     local low=string.lower(k)
     if low==rmvLowKey then
      resp.headers[k]=nil
     end
  end

  ngx.status = resp.status

  fill_response_header(resp.headers)
  util.say_and_close(resp.body)

end






